package org.example.Tomcat;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: farprt
 * @create: 2023-02-13 18:37
 **/
public class Response extends AbstractHttpServletResponse {

    private int status = 200;
    private String message = "OK";
    private byte SP = ' ';
    private byte CR = '\r';
    private byte LF = '\n';
    private Map<String, String> headers = new HashMap<>();
    private Request request;
    private OutputStream socketOutputstream;
    private ResponseServletOutputStream responseServletOutputStream = new ResponseServletOutputStream();

    public Response(Request request) {
        this.request = request;
        try {
            this.socketOutputstream = request.getSocket().getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setStatus(int i, String s) {
        this.status = i;
        this.message = s;

    }

    @Override
    public void setHeader(String s, String s1) {
        headers.put(s, s1);
    }

    @Override
    public void addHeader(String s, String s1) {
        headers.put(s, s1);
    }

    @Override
    public ResponseServletOutputStream getOutputStream() throws IOException {
        return responseServletOutputStream;
    }

    @Override
    public int getStatus() {
        return super.getStatus();
    }

    @Override
    public void setStatus(int i) {
        this.status = i;
    }

    public void complete() {
        try {
            // 发送响应
            sendResponseLine();
            sendResponseHeader();
            sendResponseBody();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void sendResponseBody() throws IOException {
        socketOutputstream.write(getOutputStream().getBytes());
    }

    private void sendResponseHeader() throws IOException {

        if(!headers.containsKey("Content-Length")){
            addHeader("Content-Length",String.valueOf(getOutputStream().getPos()));
        }
        if(!headers.containsKey("Content-Type")){
            addHeader("Content-Type", "text/plain;charset=utf-8");
        }

        for (Map.Entry<String, String> entry : headers.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            socketOutputstream.write(key.getBytes());
            socketOutputstream.write(":".getBytes());
            socketOutputstream.write(value.getBytes());
            socketOutputstream.write(CR);
            socketOutputstream.write(LF);
        }
        socketOutputstream.write(CR);
        socketOutputstream.write(LF);
    }

    private void sendResponseLine() throws IOException {
        socketOutputstream.write(request.getProtocol().getBytes());
        socketOutputstream.write(SP);
        socketOutputstream.write(status);
        socketOutputstream.write(SP);
        socketOutputstream.write(message.getBytes());
        socketOutputstream.write(CR);
        socketOutputstream.write(LF);
    }
}